home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
xlib03.zip
/
XLIBREF.DOC
< prev
next >
Wrap
Text File
|
1993-04-05
|
54KB
|
1,627 lines
-----------------------------------------------------------------------------
********* XLIB - Mode X graphics library ****************
********* ****************
********* Written By Themie Gouthas ****************
********* Aeronautical Research Laboratory ****************
********* Defence Science and Technology Organisation ****************
********* Australia ****************
********* ****************
********* egg@dstos3.dsto.gov.au ****************
********* teg@bart.dsto.gov.au ****************
Some of the code in this library has been contributed by :
Matthew MacKenzie - matm@eng.umd.edu
-----------------------------------------------------------------------------
DISCLAIMER
This library is distributed AS IS. The author/s specifically disclaim
any responsibility for any loss of profit or any incidental, consequen-
tial or other damages.
---------------------------------------------------------------------------
INTRODUCTION
---------------------------------------------------------------------------
XLIB is a "user supported freeware" graphics library specifically designed
with game programming in mind.
It has been placed in the public domain for the benefit of all, and
represents MANY hours of work so it is requested that all users comply
with the the wishes of the author/s as specified in the individual modules
and:
a) To leave the code in the public domain
b) Not distribute any modified versions of this library
New contribution and comments are welcome and hopefully there will be
more releases as the code evolves.
Finally, do not trust this excuse for a manual if in doubt. The place to get
the answers is in the code itself.
REQUIREMENTS
Minimum requirements
286 processor,
Turbo C 2.0 or higher, or BORLANDC
MAKE 2.0 or higher
TLIB 2.0 or higher
Turbo Assembler 1.01 or higher
GENERAL FEATURES
Support for a number of 256 colour tweaked graphics mode resolutions
320x200 320x240 360x200 360x240 320x400 320x480 360x400 360x480
Virtual screens larger than the physical screen (memory
permitting) that can be panned at pixel resolution in all directions
A split screen capability for status displays etc.
Text functions supporting 8x8 and 8x14 ROM fonts and user defined fonts
Support for page flipping
Graphics primitives such as line and rectangle drawing functions and
of course bit block manipulation functions
MODULES COMPRISING XLIB
XMAIN - Main module containig mode setting code and basic functions
XPOINT - Pixel functions
XRECT - Filled Rectangle and VRAM to VRAM block move functions
XPAL - Palette functions
XLINE - Line Functions
XTEXT - Text and Font Functions
XPRINTF - Printf style string output
XPBITMAP - Planar Bitmap functions
XCBITMAP - Compiled Bitmap functions
-------------------------------------------------------------------------
BUILDING THE LIBRARIES
-------------------------------------------------------------------------
Building the library had been made simple through the use of make.
To build and examples for one of the two models:
a) edit the makefile for the apropriate model (see note in the makefile)
b) edit the makefile for the apropriate compiler (again see note in the
makefile)
c) type "make" at the dos prompt.
It should be as simple as that. If problems are encountered then check
to see if tasm, make, tlib, link and bcc (or tcc) are withinin your path.
If not either change your path or specify the full path for these programs
in the makefile. It is preferrable to have your path set correctly.
Individual Compilation
----------------------
each ASM module can be compiled with the following commandline:
tasm /ml /d<model> <asm module name>
where <model> is s or l. Note the small model library can be used with
compact model programs. Similarly the large model library can be used with
medium model programs.
The resulting libraries are:
xlib<version>s.lib - small model library
xlib<version>l.lib - large model library
To link the library with your programs just include the paropriate .lib
file in your project file or on the BCC or TCC command line.
Using the library with your programs
--------------------------------------
Using the XLIB library in your programs is simple. Knowing the particular
modules you require, just include the associated header files in your program
and link your program modules with the library. If you don't want to wory
about selecting the apropriate header file then just include "XLIB_ALL.H"
which automatically includes all XLIB header files in your program.
For example compilations see the supplied makefile.
--------------------------------------------------------------------------
GLOBAL DEFINES (xlib.inc)
--------------------------------------------------------------------------
Types
BYTE unsigned char
WORD unsigned int
Available X mode resolutions
X_MODE_320x200 0
X_MODE_320x240 1
X_MODE_360x200 2
X_MODE_360x240 3
X_MODE_320x400 4
X_MODE_320x480 5
X_MODE_360x400 6
X_MODE_360x480 7
Palette rotation direction directiion
BACKWARD 0
FORWARD 1
X_MODE_INVALID -1
ERROR 1
OK 0
--------------------------------------------------------------------------
MODULE XMAIN
--------------------------------------------------------------------------
The Xmain module is the base module of the XLIB library. It contains the
essential functions that initialize and customize the graphic environment.
ASM SOURCES
xmain.asm xmain.inc xlib.inc model.inc
C HEADER FILE
xlib.h
EXPORTED VARIABLES
NOTE: All variables are read only. I you modify them the results may
be unpredictable.
InGraphics - BYTE - Flag indicating that the xlib graphics system is
active. Set by function "x_set_mode".
CurrXMode - WORD - If the xlib graphics system is active, contains the id
of the x mode. Set by function "x_set_mode".
See also defines (ie X_MODE_320x200 ... )
ScrnPhysicalByteWidth - WORD - Physical screen width in bytes. Set by
function "x_set_mode"
ScrnPhysicalPixelWidth - WORD - Physical screen width in pixels. Set by
function "x_set_mode"
ScrnPhysicalPixelHeight - WORD - Physical screen height in pixels. Set by
function "x_set_mode".
ErrorValue - WORD - Contains error value. General use variable to
communicate the error status from several functions. The value
in this variable usually is only valid for the the last
function called that sets it.
SplitScrnOffs - WORD - Offset in video ram of split screen. Set by
function "x_set_splitscrn". The value is only valid if a split
screen is active. See also global variable "SplitScrnActive".
SplitScrnScanLine - WORD - Screen Scan Line the Split Screen starts at
initially when set by function "x_set_splitscrn". The value is only
valid if a split screen is active. See also global variable
"SplitScrnActive".This variable is not updated by "x_hide_splitscrn",
"x_adjust_splitscrn".
SplitScrnVisibleHeight - WORD - The number of rows of the initial split
screen which are currently displayed. Modified by "x_hide_splitscrn",
"x_adjust_splitscrn" and "x_show_splitscrn".
Page0_Offs - WORD - Offset in video ram of main virtual screen. Initially
set by function "x_set_mode" but is updated by functions
"x_set_splitscrn" and "x_set_doublebuffer".
Page1_Offs - WORD - Offset in video ram of second virtual screen. Set by
and only is valid after a call to "x_set_doublebuffer".
ScrnLogicalByteWidth - WORD - Virtual screen width in bytes. Set by
function "x_set_mode".
ScrnLogicalPixelWidth - WORD - Virtual screen width in pixels. Set
by function "x_set_mode".
ScrnLogicalHeight - WORD - Virtual screen height in pixels. Set
initially by function "x_set_mode" but is updated by functions
"x_set_splitscrn" and "x_set_doublebuffer".
MaxScrollX - WORD - Max X pixel position of physical screen within
virtual screen. Set by function "x_set_mode".
MaxScrollY - WORD - Max Y position of physical screen within virtual
screen. Set initially by function "x_set_mode" but is updated by
functions "x_set_splitscrn" and "x_set_doublebuffer".
DoubleBufferActive - WORD - Indicates whether double-buffering is on. Set
by function "x_set_doublebuffer".
VisiblePageIdx - WORD - Index number of current visible page. Initially
set by function "x_set_doublebuffer" but is updated by "x_page_flip".
This variable is only used while double buffering is on.
HiddenPageOffs - WORD - Offset of hidden page. Initially set by function
"x_set_doublebuffer" but is updated by "x_page_flip". This variable
is only used while double buffering is on.
VisiblePageOffs - WORD - Offset of visible page. Initially set by function
"x_set_doublebuffer" but is updated by "x_page_flip". This variable
is only used while double buffering is on.
NonVisual_Offs - WORD - Offset of first byte of non-visual ram, the ram
that is available for bitmap storage etc. Set initially by function
"x_set_mode" but is updated by functions "x_set_splitscrn" and
"x_set_doublebuffer".
EXPORTED FUNCTIONS
x_set_mode
----------
C Prototype: extern WORD x_set_mode(WORD mode,WORD WidthInPixels);
mode - The required mode as defined by the "Available X Mode
resolutions" set of defines in the xlib.h header file.
WidthInPixels - The required virtual screen width.
Returns - The actual width in pixels of the allocated virtual
screen
This function initialises the graphics system, setting the apropriate
screen resolution and allocating a virtual screen. The virtual screen
allocated may not necessarily be of the same size as specified in the
"WidthInPixels" parameter as it is rounded down to the nearest
multiple of 4.
The function returns the actual width of the allocated virtual screen
in pixels if a valid mode was selected otherwise returns
X_MODE_INVALID.
Saves virtual screen pixel width in "ScrnLogicalPixelWidth".
Saves virtual screen byte width in "ScrnLogicalByteWidth".
Physical screen dimensions are set in "ScrnPhysicalPixelWidth".
"ScrnPhysicalByteWidth" and "ScrnPhysicalHeight". Other global
variables set are "CurrXMode","MaxScrollX", "MaxScrollY",
"InGraphics". The variable "SplitScrnScanline" is also initialized
to zero.
See also:
Available X Mode resolutions
What is Mode X
x_select_default_plane
----------------------
C Prototype: void x_select_default_plane(BYTE plane);
Enables default Read/Write access to a specified plane
x_set_splitscreen
-----------------
C Prototype: extern void x_set_splitscreen(WORD line);
line - The starting scan line of the required split screen.
This function activates Mode X split screen and sets starting scan
line. The split screen resides on the bottom half of the screen and has
a starting address of A000:0000 in video RAM.
It also Updates Page0_Offs to reflect the existence of the split screen
region ie "MainScrnOffset" is set to the offset of the first pixel
beyond the split screen region. Other variable set are "Page1_Offs" which
is set to the same value as "Page0_Offs" (see graphics call sequence
below), "ScrnLogicalHeight","ScrnPhysicalHeight", "SplitScrnScanLine" and
"MaxScrollY".
This function cannot be called after double buffering has been activated,
it will return an error. To configure your graphics environment the
sequence of graphics calls is as follows although either or both steps b
and c may be omitted:
a) x_set_mode
b) x_set_splitscreen
c) x_set_doublebuffer
Thus when you call this function successfully, double buffering is not
active so "Page1_Offs" is set to the same address as "Page0_Offs".
WARNING: If you use one of the high resolution modes (360x480 as an
extreme example) you may not have enough video ram for split screen
and double buffering options since VGA video RAM is restricted to
64K.
See Also:
What is a Split Screen ?
What is double buffering ?
x_set_doublebuffer
------------------
C Prototype: extern WORD x_set_doublebuffer(WORD PageHeight);
PageHeight - The height of the two double buffering virtual screens.
Returns - The closest possible height to the specified.
This function sets up two double buffering virtual pages. 'ErrorValue"
is set according to the success or failure of this command.
Other variables set are:
_Page1_Offs Offset of second virtual page
_NonVisual_Offs Offset of first non visible video ram byte
_DoubleBufferActive Flag
_PageAddrTable Table of Double buffering pages start offsets
_ScrnLogicalHeight Logical height of the double buffering pages
_MaxScrollY Max vertical start address of physical screen
within the virtual screen
WARNING: If you use one of the high resolution modes (360x480 as an
extreme example) you may not have enough video ram for split screen
and double buffering options since VGA video RAM is restricted to
64K.
See Also:
What is double buffering ?
x_hide_splitscreen
------------------
C Prototype: extern void x_hide_splitscreen(void);
This function hides an existing split screen by setting its starting
scan line to the last physical screen scan line.
"ScreenPhysicalHeight" is adjusted but the "SplitScreenScanLine" is not
altered as it is required for restoring the split screen at a later stage.
WARNING: Only to be used if SplitScrnLine has been previously called
Disabled for mode 4-7 (320x400-360x480). The memory for
the initial split screen is reserved and the size limitations
of these modes means any change in the split screen scan line
will encroach on the split screen ram
Update: Now disabled for these modes
See Also:
What is a split screen ?
x_show_splitscreen
------------------
C Prototype: extern void x_show_splitscreen(void);
Restores split screen start scan line to the initial split screen
starting scan line as set by "SplitScrnScanLine".
"ScreenPhysicalHeight" is adjusted.
WARNING: Only to be used if SplitScrnLine has been previously called
Disabled for mode 4-7 (320x400-360x480). The memory for
the initial split screen is reserved and the size limitations
of these modes means any change in the split screen scan line
will encroach on the split screen ram
x_adjust_splitscreen
--------------------
C Prototype: extern void x_adjust_splitscreen(WORD line);
line - The scan line at which the split screen is to start.
Sets the split screen start scan line to a new scan line. Valid scan lines
are between the initial split screen starting scan line and the last
physical screen scan line. "ScreenPhysicalHeight" is also adjusted.
WARNING: Only to be used if SplitScrnLine has been previously called
Disabled for mode 4-7 (320x400-360x480). The memory for
the initial split screen is reserved and the size limitations
of these modes means any change in the split screen scan line
will encroach on the split screen ram
x_set_start_addr
----------------
C Prototype: extern void x_set_start_addr(WORD X,WORD Y);
X,Y - coordinates of top left corner of physical screen within current
virtual screen.
Set Mode X non split screen physical start address within current virtual
page.
X must not exceed (Logical screen width - Physical screen width)
ie "MaxScrollX" and Y must not exceed (Logical screen height -
Physical screen height) ie "MaxScrollY"
x_page_flip
-----------
C Prototype: extern void x_page_flip(WORD X,WORD Y);
X,Y - coordinates of top left corner of physical screen within the
the hidden virtual screen if double buffering is active, or
the current virtual screen otherwise.
Sets the physical screen start address within currently hidden virtual
page and then flips pages. If double buffering is not active then this
function is functionally equivalent to "x_set_start_addr".
X must not exceed (Logical screen width - Physical screen width)
ie "MaxScrollX" and Y must not exceed (Logical screen height -
Physical screen height) ie "MaxScrollY"
x_text_mode
-----------
C Prototype: extern void x_text_mode(void);
Disables graphics mode.
--------------------------------------------------------------------------
MODULE XPOINT
--------------------------------------------------------------------------
Point functions all MODE X 256 Color resolutions
ASM SOURCES
xpoint.asm xpoint.inc xlib.inc model.inc
C HEADER FILE
xpoint.h
EXPORTED FUNCTIONS
x_put_pix
---------
C Prototype: extern void x_put_pix(WORD X,WORD Y,WORD PageOffset,
WORD Color);
Draw a point of specified colour at coordinates X,Y
within the virtual page starting at offset PageOffset.
x_get_pix
---------
C Prototype: extern WORD x_get_pix(WORD X, WORD Y, WORD PageBase);
Read a point of at coordinates X,Y within the virtual page starting
at offset PageOffset.
--------------------------------------------------------------------------
MODULE XRECT
--------------------------------------------------------------------------
Screen rectangle display and manipulation functions
ASM SOURCES
xrect.asm xrect.inc xlib.inc model.inc
C HEADER FILE
xrect.h
EXPORTED FUNCTIONS
x_rect_pattern
--------------
C Prototype: extern void x_rect_pattern(WORD StartX, WORD StartY,
WORD EndX, WORD EndY,
WORD PageBase,BYTE far *Pattern);
StartX,StartY - Coordinates of upper left hand corner of rectangle
EndX,EndY - Coordinates of lower right hand corner of rectangle
PageBase - Offset of virtual screen
*Pattern - Pointer to the user defined pattern (16 bytes)
Mode X rectangle 4x4 pattern fill routine.
Upper left corner of pattern is always aligned to a multiple-of-4
row and column. Works on all VGAs. Uses approach of copying the
pattern to off-screen display memory, then loading the latches with
the pattern for each scan line and filling each scan line four
pixels at a time. Fills up to but not including the column at EndX
and the row at EndY. No clipping is performed.
Based on code originally published in DDJ Mag by M. Abrash
Warning the VGA memory locations PATTERN_BUFFER (A000:FFFc) to
A000:FFFF are reserved for the pattern buffer
See Also:
Doctor Dobbs Journal references.
x_rect_fill
------------
C Prototype: extern void x_rect_fill(WORD StartX,WORD StartY,
WORD EndX,WORD EndY,
WORD PageBase,WORD color);
StartX,StartY - Coordinates of upper left hand corner of rectangle
EndX,EndY - Coordinates of lower right hand corner of rectangle
PageBase - Offset of virtual screen
Color -color to use for fill
Mode X rectangle solid color fill routine.
Based on code originally published in DDJ Mag by M. Abrash
See Also:
Doctor Dobbs Journal references.
x_cp_vid_rect
--------------
C Prototype: extern void x_cp_vid_rect(WORD SourceStartX,WORD SourceStartY,
WORD SourceEndX,WORD SourceEndY,
WORD DestStartX,WORD DestStartY,
WORD SourcePageBase,WORD DestPageBase,
WORD SourceBitmapWidth,WORD DestBitmapWidth);
StartX,StartY- Coordinates of upper left hand corner of source rectangle
EndX,EndY - Coordinates of lower right hand corner of source rectangle
DestStartX,DestStartY - Coordinates of rectangle destination
SourcePageBase - source rectangle page offset
DestPageBase - destination rectangles page offset
SourceBitmapWidth - width of bitmap within the source virtual screen
containing the source rectangle
DestBitmapWidth - width of bitmap within the dest. virtual screen
containing the destination rectangle
Mode X display memory to display memory copy
routine. Left edge of source rectangle modulo 4 must equal left edge
of destination rectangle modulo 4. Works on all VGAs. Uses approach
of reading 4 pixels at a time from the source into the latches, then
writing the latches to the destination. Copies up to but not
including the column at SrcEndX and the row at SrcEndY. No
clipping is performed. Results are not guaranteed if the source and
destination overlap.
Based on code originally published in DDJ Mag by M. Abrash
See Also:
Doctor Dobbs Journal references.
---------------------------------------------------------------------------
MODULE XPAL
---------------------------------------------------------------------------
Palette functions for VGA 256 color modes.
All the functions in this module operate on two variations of the
pallete buffer, the raw and annotated buffers.
All those functions ending in "raw" operate on the following palette
structure:
BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
No reference to the starting colour index or number of colours stored
is contained in the structure.
All those functions ending in "struc" operate on the following palette
structure:
BYTE:c,BYTE:n,BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
where c is the starting colour and n is the number of colours stored
WARNING: There is no validity checking in these functions. The onus is
on the user to supply valid parameters to the functions.
ASM SOURCES
xpal.asm xpal.inc xlib.inc model.inc
C HEADER FILE:
xpal.h
EXPORTED FUNCTIONS
x_get_pal_raw
-------------
C Prototype: extern void x_get_pal_raw(BYTE far * pal,WORD num_colrs,
WORD start_index);
Read DAC palette into raw buffer with interrupts disabled
ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
WARNING: Memory for the palette buffers must all be pre-allocated.
x_get_pal_struc
---------------
C Prototype: extern void x_get_pal_struc(BYTE far * pal,WORD num_colrs,
WORD start_index);
Read DAC palette into annotated type buffer with interrupts disabled
ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
WARNING: memory for the palette buffers must all be pre-allocated
x_put_pal_raw
-------------
C Prototype: extern void x_put_pal_raw(BYTE far * pal,WORD num_colrs,
WORD start_index);
Write DAC palette from raw buffer with interrupts disabled
ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
x_put_pal_struc
--------------
C Prototype: extern void x_put_pal_struc(BYTE far * pal);
Write DAC palette from annotated type buffer with interrupts disabled
ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
x_set_rgb
---------
C Prototype: extern x_set_rgb(BYTE color,BYTE red_c,BYTE green_c,
BYTE blue_c);
Set the RGB components of a vga color
x_rot_pal_struc
---------------
C Prototype: extern void x_rot_pal_struc(BYTE far * pal,WORD direction);
Rotate annotated palette buffer entries. Direction 0 = backward,
1 = forward.
x_rot_pal_raw
-------------
C Prototype: extern x_rot_pal_raw(BYTE far * pal,WORD direction,
WORD num_colrs);
Rotate a raw palette buffer. Direction 0 = backward,
1 = forward.
x_put_contrast_pal_struc
------------------------
C Prototype: extern void x_put_contrast_pal_struc(BYTE far * pal,
BYTE intensity);
Write DAC palette from annotated type buffer with specified intensity
adjustment (ie palette entries are decremented where possible by
"intensity" units).
Designed for fading in or out a palette without using an intermediate
working palette buffer ! (Slow but memory efficient ... OK for small
pal strucs}
x_transpose_pal_struc
---------------------
C Prototype: extern void x_transpose_pal_struc(BYTE far * pal,
WORD StartColor);
Write DAC palette from annotated type buffer with interrupts disabled
starting at a new palette index.
x_cpcontrast_pal_struc
----------------------
C Prototype: extern WORD x_cpcontrast_pal_struc(BYTE far *src_pal,
BYTE far *dest_pal,BYTE Intensity);
Copy one annotated palette buffer to another making the intensity
adjustment. Used in fading in and out fast and smoothly.
---------------------------------------------------------------------------
MODULE XLINE
---------------------------------------------------------------------------
Line Drawing functions.
ASM SOURCES
xline.asm xline.inc xlib.inc model.inc
C HEADER FILE
xline.h
EXPORTED FUNCTIONS
x_line
------
C Prototype: extern void x_line(WORD x0,WORD y0,WORD x1,WORD y1,
WORD color,WORD PageBase);
Draw a line with the specified end points in the page starting at
offset "PageBase".
No Clipping is performed.
---------------------------------------------------------------------------
MODULE XTEXT
---------------------------------------------------------------------------
ASM SOURCES
xtext.asm xtext.inc xlib.inc model.inc
C HEADER FILE
xtext.h
DEFINES
FONT_8x8 0
FONT_8x15 1
FONT_USER 2
EXPORTED VARIABLES
NOTE: All variables are read only. I you modify them the results may
be unpredictable.
CharHeight - BYTE - Height of current inbuilt character set
CharWidth - BYTE - Width of current inbuilt character set
FirstChar - BYTE - First character of current inbuilt character set
UserCharHeight - BYTE - Height of current user character set
UserCharWidth - BYTE - Width of current user character set
UserFirstCh - BYTE - First character of current user character set
EXPORTED FUNCTIONS
x_text_init
-----------
C Prototype: extern WORD x_text_init(void);
Initializes the Mode X text driver and sets the default font (VGA ROM 8x8)
x_set_font
----------
C Prototype: extern void x_set_font(WORD FontId);
Select the working font where 0 = VGA ROM 8x8, 1 = VGA ROM 8x14
2 = User defined bitmapped font.
WARNING: A user font must be registered before setting FontID 2
See Also:
Defines for this module
x_register_userfont
-------------------
C Prototype: extern void x_register_userfont(char far *UserFontPtr);
Register a user font for later selection. Only one user font can be
registered at any given time. Registering a user font deregisters the
previous user font. User fonts may be at most 8 pixels wide.
USER FONT STRUCTURE
Word: ascii code of first char in font
Byte: Height of chars in font
Byte: Width of chars in font
n*h*Byte: the font data where n = number of chars and h = height
of chars
WARNING: The onus is on the program to ensure that all characters
drawn whilst this font is active, are within the range of
characters defined.
x_put_char
----------
C Prototype: extern void x_put_char(char ch,WORD X,WORD Y,WORD PgOffs,
WORD Color);
Draw a text character at the specified location with the specified
color.
ch - char to draw
x,y - screen coords at which to draw ch
ScrnOffs - Starting offset of page on whih to draw
Color - Color of the text
WARNING: InitTextDriver must be called before using this function
**** NOTE ****
The file "xprintf.c" implements a printf style formatted output function
x_printf
--------
C Prototype: void x_printf(int x,int y,unsigned ScrnOffs,int color,
char *ln,...);
x,y - screen coords at which to draw ch
ScrnOffs - Starting offset of page on whih to draw
Color - Color of the text
Parameters beyond Color conform to the standard printf parameters.
x_bgprintf
----------
C Prototype: void x_bgprintf(int x,int y,unsigned ScrnOffs,int fgcolor,
int bgcolor, char *ln,...);
x,y - screen coords at which to draw ch
ScrnOffs - Starting offset of page on whih to draw
fgcolor - Color of the text foreground
bgcolor - Color of the text background
Parameters beyond bgolor conform to the standard printf parameters.
--------------------------------------------------------------------------
MODULE XPBITMAP
--------------------------------------------------------------------------
This module implements a set of functions to operate on planar bitmaps.
Planar bitmaps as used by these functions have the following structure:
BYTE 0 The bitmap width in bytes (4 pixel groups) range 1..255
BYTE 1 The bitmap height in rows range 1..255
BYTE 2..n1 The plane 0 pixels width*height bytes
BYTE n1..n2 The plane 1 pixels width*height bytes
BYTE n2..n3 The plane 2 pixels width*height bytes
BYTE n3..n4 The plane 3 pixels width*height bytes
These functions provide the fastest possible bitmap blts from system ram to
to video and further, the single bitmap is applicable to all pixel
allignments. The masked functions do not need separate masks since all non
zero pixels are considered to be masking pixels, hence if a pixel is 0 the
corresponding screen destination pixel is left unchanged.
ASM SOURCES
xpbitmap.asm xpbitmap.inc xlib.inc model.inc
C HEADER FILE
xpbitmap.h
EXPORT FUNCTIONS
x_put_masked_pbm
----------------
C Prototype: extern void x_put_masked_pbm(WORD X,WORD Y,WORD ScrnOffs,
BYTE far * Bitmap);
Mask write a planar bitmap from system ram to video ram. All zero source
bitmap bytes indicate destination byte to be left unchanged.
Source Bitmap structure:
Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
Bitmap data (plane 2)..,Bitmap data (plane 3)..
NOTE: width is in bytes ie lots of 4 pixels
LIMITATIONS: No clipping is supported
Only supports bitmaps with widths which are a multiple of
4 pixels
See Also: XBMTOOLS module for linear <-> planar bitmap conversion
functions.
x_put_pbm
---------
C Prototype: extern void x_put_pbm(WORD X, WORD Y, WORD ScrnOffs,
BYTE far * Bitmap);
Write a planar bitmap from system ram to video ram.
Source Bitmap structure:
Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
Bitmap data (plane 2)..,Bitmap data (plane 3)..
NOTE: width is in bytes ie lots of 4 pixels
LIMITATIONS: No clipping is supported
Only supports bitmaps with widths which are a multiple of
4 pixels
FEATURES : Automatically selects REP MOVSB or REP MOVSW depending on
source bitmap width, by modifying opcode ;-).
See Also: XBMTOOLS module for linear <-> planar bitmap conversion
functions.
x_get_pbm
---------
C Prototype: extern void x_get_pbm(WORD X, WORD Y,BYTE Bw,BYTE Bh,
WORD ScrnOffs, BYTE far * Bitmap);
Read a planar bitmap to system ram from video ram.
Source Bitmap structure:
Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
Bitmap data (plane 2)..,Bitmap data (plane 3)..
NOTE: width is in bytes ie lots of 4 pixels
LIMITATIONS: No clipping is supported
Only supports bitmaps with widths which are a multiple of
4 pixels
FEATURES : Automatically selects REP MOVSB or REP MOVSW depending on
source bitmap width, by modifying opcode ;-).
See Also: XBMTOOLS module for linear <-> planar bitmap conversion
functions.
--------------------------------------------------------------------------
MODULE XCBITMAP
--------------------------------------------------------------------------
XCBITMAP:
The Care and Feeding of Compiled Masked Blits
by Matthew MacKenzie
This file describes the XCBITMAP module of the Xlib library, made up of
the files XCBITMAP.ASM, XCBITMAP.INC, XCBITMAP.H, and this file, XCBITMAP.DOC.
The XCBITMAP module is small, containing only two procedures:
o x_compile_bitmap compiles your bitmap into native code which writes
to the VGA screen in an X mode.
o x_put_cbitmap converts X and Y coordinates into a location on the
screen, sets up the necessary VGA registers, and executes the compiled
bitmap as a subroutine.
x_compile_bitmap scans through a source bitmap and generates 8086
instructions to plot every nonzero pixel. It is designed to be used
before the action begins rather than on-the-fly. The compiled bitmap
contains no branches, and no reference to the zero (transparent) pixels.
Where two pixels are exactly four columns apart they are plotted with a
single 16-bit store, and the VGA MAP_MASK register will be set at most
four times. As a result your bitmap may run several times faster than a
traditional memory-to-VGA masked blit routine.
There is no way to perform clipping on these bitmaps, or to plot a
pixel of color zero.
x_compile_bitmap works with bitmaps in a very simple format, and does
not require the bitmap's width or height to be multiples of four, eight,
or even two. While this makes it easier for us humans to design new
bitmaps, it does make the compilation relatively slow, on a time scale of
60 frames per second. Since a compiled bitmap is relocatable you may
just want to have it saved to disk, and not include the source bitmap
in your program at all.
The source bitmap format is an array of bytes, like this:
char far * eye ={
13,7,
0, 0, 0, 0, 9, 1, 1, 1, 9, 0, 0, 0, 0,
0, 0, 9, 9, 1, 1, 1, 4, 4, 9, 9, 0, 0,
0, 9, 9, 1, 2, 0, 0, 4, 4, 1, 9, 9, 0,
9, 9, 9, 1, 0, 0, 0, 0, 1, 1, 9, 9, 9,
0, 9, 9, 1, 2, 0, 0, 2, 1, 1, 9, 9, 0,
0, 0, 9, 9, 1, 1, 1, 1, 1, 9, 9, 0, 0,
0, 0, 0, 0, 9, 1, 1, 1, 9, 0, 0, 0, 0};
To compile this image for a mode 360 pixels (90 byte columns) across:
EyeSize = x_compile_bitmap(90, eye, CompiledEye);
Both `eye' and `CompiledEye' are of type (char far *). Notice that
both buffers must exist beforehand. Since x_compile_bitmap returns the
size of the compiled code, in bytes, you can reallocate the bitmap
immediately to the right size. The pointers are 32-bit because
compiled bitmaps take so much space: they are at one end of the speed-
versus-memory spectrum. You may, of course, still use a 16-bit memory
model, or your own allocation scheme, and cast the pointers to 32 bits
to compile or blit a bitmap. A good rule of thumb is to allocate
(3.5 x buffer-height x buffer-width) + 25 bytes (rounding up ;-),
then pare your bitmap down when you find out how much space you've
actually used.
Since the compiled bitmap has to fit within one segment of memory, it
cannot contain more than about 19,000 pixels. This will not be a
limitation for most sane programmers. If you are not a sane programmer try
splitting your huge, unwieldy image up into smaller parts -- you can use
the same gigantic bitmap if you divide it into horizontal slices for
compilation. For that matter, dividing the source up that way will let
you use a source bitmap large than 64K, which is an even sicker idea...
Back to business. A bitmap is compiled for only one width of screen.
If you are using a logical screen larger than your physical screen, call
the bitmap compiler with the logical width -- the important thing is the
number of bytes per line. Notice that you do not have to be in a graphics
mode to use this routine. This allows you to develop and compile bitmaps
separately, with whatever utility programs you might cook up.
The second function is x_put_cbitmap. To plot our eye at (99,4), on
the page which starts at location 0:
x_put_cbitmap(99, 4, 0, CompiledEye);
This function depends on the global variable ScrnLogicalByteWidth from
the module XMAIN, which should be the same number as the column parameter
you used to compile your bitmap.
The XCBITMAP module supports memory-to-VGA blits only. Xlib also
includes non-masking routines which can quickly save and restore the
background screen behind your bitmap, using fast string operations.
This module is part of the Xlib package, and is in the public domain.
If you write something which uses it, though, please send me a copy as a
courtesy -- if for no other reason so I can tilt my chair back and reflect
that it may have been worth the trouble after all.
The included program DEMO2.C demonstarates the performance difference
between planar bitmap masked blits and compiled bitmap blits.
ASM SOURCES
xcbitmap.asm xcbitmap.inc xlib.inc model.inc
C HEADER FILE
xcbitmap.h
EXPORT FUNCTIONS
x_compile_bitmap
----------------
C Prototype: extern int x_compile_bitmap (WORD scrn_logical_width,
char far * bitmap,char far * output);
Compile a linear bitmap to generate machine code to plot it
at any required screen coordinates FAST. Faster than blits
using planar bitmaps as in module XPBIITMAP
The screen logical width is in bytes rather than pixels.
The source linear bitmaps have the following structure:
BYTE 0 The bitmap width in pixels range 1..255
BYTE 1 The bitmap height in rows range 1..255
BYTE 2..n The width*height bytes of the bitmap
All four main registers are totaled.
See Also: XBMTOOLS module for linear <-> planar bitmap conversion
functions.
x_put_cbitmap
-------------
C Prototype: extern void x_put_cbitmap (WORD XPos,WORD YPos,
WORD PageOffset,char far * CompiledBitmap);
XPos,YPos - screen coords at which to draw bitmap
PageOffs - Starting offset of page on whih to draw
CompiledBitmap - pointer to compiled bitmap
Displays a compiled bitmap generated by x_compile_bitmap at given
coordinates, on a given screen page.
See Also: XBMTOOLS module for linear <-> planar bitmap conversion
functions.
--------------------------------------------------------------------------
MODULE XBMTOOLS
--------------------------------------------------------------------------
This module implements a set of functions to convert between planar
bitmaps and linear bitmaps.
PLANAR BITMAPS
Planar bitmaps as used by these functions have the following structure:
BYTE 0 The bitmap width in bytes (4 pixel groups) range 1..255
BYTE 1 The bitmap height in rows range 1..255
BYTE 2..n1 The plane 0 pixels width*height bytes
BYTE n1..n2 The plane 1 pixels width*height bytes
BYTE n2..n3 The plane 2 pixels width*height bytes
BYTE n3..n4 The plane 3 pixels width*height bytes
as used by x_put_pbm, x_get_pbm, x_put_masked_pbm.
LINEAR BITMAPS
Linear bitmaps have the following structure:
BYTE 0 The bitmap width in pixels range 1..255
BYTE 1 The bitmap height in rows range 1..255
BYTE 2..n The width*height bytes of the bitmap
ASM SOURCES
xbmtools.asm xpbmtools.inc model.inc
C HEADER FILE
xbmtools.h
EXPORT DEFINES
BM_WIDTH_ERROR
EXPORT FUNCTIONS
x_pbm_to_bm
------------
C Prototype: extern int x_pbm_to_bm(char far * source_pbm,
char far * dest_bm);
This function converts a bitmap in the planar format to the linear format
as used by x_compile_bitmap.
WARNING: the source and destination bitmaps must be pre - allocated
NOTE: This function can only convert planar bitmaps that are suitable.
If the source planar bitmap's width (per plane) is >= 256/4
it cannot be converted. In this situation an error code
BM_WIDTH_ERROR. On successful conversion 0 is returned.
x_bm_to_pbm
------------
C Prototype: extern int x_bm_to_pbm(char far * source_pbm,
char far * dest_bm);
This function converts a bitmap in the linear format as used by
x_compile_bitmap to the planar formap.
WARNING: the source and destination bitmaps must be pre - allocated
NOTE: This function can only convert linear bitmaps that are suitable.
If the source linear bitmap's width is not a multiple of 4
it cannot be converted. In this situation an error code
BM_WIDTH_ERROR. On successful conversion 0 is returned.
--------------------------------------------------------------------------
MODULE XDETECT
--------------------------------------------------------------------------
This module implements a set of functions to detect the PC's hardware
configuration.
ASM SOURCES
xdetect.asm xdetect.inc model.inc
C HEADER FILE
xdetect.h
EXPORTED DEFINES
I8086 0
I80186 1
I80286 2
I80386 3
NoGraphics 0
MDA 1
CGA 2
EGAMono 3
EGAColor 4
VGAMono 5
VGAColor 6
MCGAMono 7
MCGAColor 8
EXPORT FUNCTIONS
x_graphics_card
---------------
C Prototype: extern int x_graphics_card();
This function returns the type of graphics card installed. See defines
above.
x_processor
-----------
C Prototype: extern int x_processor();
This function returns the type of processor installed. A 486 registers
as a 386. See defines above.
--------------------------------------------------------------------------
MODULE XFILEIO
--------------------------------------------------------------------------
Handle based file I/O functions.
See any good DOS programming reference for more information on int 21h
DOS services.
ASM SOURCES
xfileio.asm xfileio.inc model.inc
C HEADER FILE
xfileio.h
EXPORTED DEFINES
file access modes
F_RDONLY
F_WRONLY
F_RDWR
seek codes
SEEK_START
SEEK_CURR
SEEK_END
file error value
FILE_ERR
EXPORT FUNCTIONS
f_open
------
C Prototype: extern int f_open(char * filename, char access);
Opens a file according to the access char:
F_RDONLY = read only - If doesnt exist return error
F_WRONLY = write only - If doesnt exist create it otherwise clear it
F_RDWR = read/write - If doesnt exist create it
Returns the file handle on success, FILE_ERR on failure
f_close
-------
C Prototype: extern int f_close(int handle);
Closes the file associated with the specified handle
Returns 0 on success, FILE_ERR on failure
f_read
------
C Prototype: extern int f_read(int handle, char far * buffer, int count);
Reads a block of count bytes from the file specified by the handle
into the buffer
Returns count on success, FILE_ERR on failure
f_write
-------
C Prototype: extern int f_write(int handle, char far * buffer, int count);
Writes a block of count bytes to the file specified by the handle
from the buffer
Returns count on success, FILE_ERR on failure
f_seek
------
C Prototype: extern long int f_seek(int handle, long int position,
char method_code)
Moves the file pointer according to the position and method code
Returns file pointer position on success, FILE_ERR on failure
f_filelength
------------
C Prototype:
extern long int f_filelength(int handle)
Returns the length of the file associated with the specified handle
Returns file length on success, FILE_ERR on failure
--------------------------------------------------------------------------
MODULE XRLETOOL
--------------------------------------------------------------------------
This module implements a number of functions comprising an RLE encoding
decoding system.
RLE stands for RUN LENGTH ENCODING. It is a quick simple data compression
scheme which is commonly used for image data compression or compression
of any data. Although not the most efficient system, it is fast, which is
why it is used in image storage systems like PCX. This implementation is
more efficient than the one used in PCX files because it uses 1 bit to
identify a Run Length byte as opposed to two in PCX files, but more on this
later.
This set of functions can be used to implement your own compressed image
file format or for example compress game mapse for various levels etc.
The uses are limited by your imagination.
I opted for trading off PCX RLE compatibility for the improved compression
efficiency.
Here is how the data is un-compressed to give an idea of its structure.
STEP 1 read a byte from the RLE compressed source buffer.
STEP 2 if has its high bit is set then the lower 7 bits represent the number
of times the next byte is to be repeated in the destination buffer.
if the count (lower 7 bits) is zero then
we have finished decoding goto STEP 5
else goto STEP 4
STEP 3 Read a data from the source buffer and copy it directly to the
destination buffer.
goto STEP 1
STEP 4 Read a data from the source buffer and copy it to the destination
buffer the number of times specified by step 2.
goto STEP 1
STEP 5 Stop, decoding done.
If the byte does not have the high bit set then the byte itself is transfered
to the destination buffer.
Data bytes that have the high bit already set and are unique in the input
stream are represented as a Run Length of 1 (ie 81 which includes high bit)
followed by the data byte.
If your original uncompressed data contains few consecutive bytes and most
have high bit set (ie have values > 127) then your so called
compressed data would require up to 2x the space of the uncompressed data,
so be aware that the compression ratio is extremely variable depending on the
type of data being compressed.
Apologies for this poor attempt at a description, but you can look up
RLE in any good text. Alternatively, any text that describes the PCX file
structure in any depth should have a section on RLE compression.
ASM SOURCES
xrletool.asm xrletool.inc model.inc
C HEADER FILE
xrletool.h
EXPORTED DEFINES
EXPORT FUNCTIONS
x_buff_RLDecode
---------------
Expands an RLE compresses source buffer to a destination buffer.
returns the size of the resultant uncompressed data.
C PROTOTYPE:
extern unsigned int x_buff_RLDecode(char far * source_buff,
char far * dest_buff);
source_buff - The buffer to compress
dest_buff - The destination buffer
WARNING: buffers must be pre allocated.
x_buff_RLEncode
---------------
RLE Compresses a source buffer to a destination buffer and returns
the size of the resultant compressed data.
C PROTOTYPE:
extern unsigned int x_buff_RLEncode(char far * source_buff,
char far * dest_buff,unsigned int count);
source_buff - The buffer to compress
dest_buff - The destination buffer
count - The size of the source data in bytes
WARNING: buffers must be pre allocated.
x_buff_RLE_size
---------------
Returns the size the input data would compress to.
C PROTOTYPE:
extern unsigned int x_buff_RLE_size(char far * source_buff,
unsigned int count);
source_buff - The uncompressed data buffer
count - The size of the source data in bytes
x_file_RLEncode
---------------
RLE Compresses a source buffer to an output file returning
the size of the resultant compressed data or 0 if it fails.
C PROTOTYPE:
extern unsigned int x_file_RLEncode(int handle,
char far * source_buff,unsigned int count);
source_buff - The buffer to compress
handle - The file handler
count - The size of the source data in bytes
x_file_RLDecode
---------------
Expands an RLE compresses file to a destination RAM buffer.
returns the size of the resultant uncompressed data.
C PROTOTYPE:
extern unsigned int x_buff_RLDecode(int handle,
char far * dest_buff);
handle - Input file handle
dest_buff - The destination buffer
--------------------------------------------------------------------
REFERENCE SECTION
--------------------------------------------------------------------
REFERENCES
----------
In my opinion Doctor Dobbs Journal is the best reference text for
VGA Mode X graphics:
Issue 178 Jul 1991 : First reference to Mode X
Article Abstract : VGA's undocumented Mode X supports page flipping,
makes off screen memory available, has square pixels,
and increases performance by as muck as 4 times.
Issue 179 Aug 1991 : Continuation
Article Abstract : Michael discusses latches and VGA's undoccumented
Mode X.
Issue 181 Sep 1991 : Continuation
Article Abstract : Michael puts the moves on animation using VGA's 256
colors.
Issue 184 Oct 1991 : First of a continuing series covering 3-D animation
using VGA's Mode X. This series is still ongoing
(October 1992)
Article Abstract : Michael moves into 3-D animation, starting with basic
polygon fills and page flips.
WHAT IS MODE X ?
----------------
Mode X is a derrivative of the VGA's standard mode 13h (320x200 256 color).
It is a (family) of undocumented video modes that are created by tweaking
the VGA's registers. The beauty of mode X is that it offers several
benefits to the programmer:
- Multiple graphice pages where mode 13h doesn't allowing for page flipping
(also known as double buffering) and storage of images and data in
offscreen video memory
- A planar video ram organization which although more difficult to program,
allows the VGA's plane-oriented hardware to be used to process pixels in
parallel, improving performance by up to 4 times over mode 13h
See issue 178-179 of D.D.J. for a full description of VGA's Mode X.
WHAT IS A SPLIT SCREEN ?
------------------------
A split screen is a neat hardware feature offered by the EGA and VGA video
cards. A split screen is a mode of graphics operationin which the Hardware
splits the visual graphics screen horizontally and treats both halves as
individual screens each starting at different locations in video RAM.
The bottom half (which is usually referred to as the split screen) always
starts at address A000:0000 but the top half's starting address is user
definable.
The most common application of split screens in games is the status display
in scrolling games. Split screens make this sort of game simpler to program
because when the top half window is scrolled the programmer does not have to
worry about redrawing the bottom half.
WHAT IS DOUBLE BUFFERING ?
--------------------------
Double buffering (also known as page flipping) is the technique most often
used to do animation. it requires hardware that is capable of displaying
multiple graphics pages (or at least 2). Animation is achieved by drawing
an image in the non visible screen and then displaying the non visible
screen. Once te page has been flipped the process starts again. The next
frame of the animation is drawn on the non visible screen, the page is
flipped again etc.
iMode X is a derrivative of the VGA's standard mode 13h (320x200 256 color).
It is a (family) of undocumented video modes that are created by tweaking
the VGA's registers. The beauty of mode X is that it offers several
benefits to the programmer:
- Multiple graphice pages where mode 13h doesn't allowing for page flipping
(also known as double buffering) and storage of images and data in
offscreen video memory
- A planar video ram organization which although more difficult to program,
allows the VGA's plane-oriented hardware to be used to process pixels in
parallel, improving performance by up to 4 times over mode 13h
Again see D.D.J. for an in depth discussion of animation using Mode X